#!user/bin/python3
# -*- coding:UTF-8 -*-

################################################################################
#
# Copyright (c) 2020 openEuler.org, Inc. All Rights Reserved
#
################################################################################
"""
Handle common tasks

Authors: zhangjianjun
Date:    05/08/2021 11:01 AM
"""

from dbConnecttion.MysqlConn import Mysql


def select_openeuler_yaml_data(mysql):
    """
    Query data
    """
    sql = "SELECT package_id, git_id,package_name,version FROM cve_git_open_euler " \
          "WHERE package_id >= %s"
    val = (10000000,)
    packages_data = mysql.getMany(sql, val)
    if packages_data and len(packages_data) > 0 and len(packages_data[0]) > 0:
        return packages_data
    else:
        return None


def delete_yaml_openeuler_data(mysql, git_id):
    """
    delete origin data
    """
    delete_sql = "delete from cve_git_open_euler where git_id = %s"
    val = (git_id,)
    mysql.delete(delete_sql, val)
    mysql.dispose()


def delete_yaml_openeuler_detail_data(mysql, git_id):
    """
    delete origin data
    """
    delete_sql = "delete from cve_git_package_info where git_id = %s"
    val = (git_id,)
    mysql.delete(delete_sql, val)
    mysql.dispose()


def select_mindspore_yaml_origin_data(package_name, version, mysql):
    """
    Query origin data
    """
    sql = "SELECT id FROM cve_mind_spore_yaml WHERE package_name = %s " \
          "and version = %s"
    val = (package_name, version)
    packages_data = mysql.getOne(sql, val)
    if packages_data:
        return packages_data
    else:
        return None


def select_gauss_yaml_origin_data(package_name, version, mysql):
    """
    Query origin data
    """
    sql = "SELECT id FROM cve_open_guss_yaml WHERE package_name = %s " \
          "and version = %s"
    val = (package_name, version)
    packages_data = mysql.getOne(sql, val)
    if packages_data:
        return packages_data
    else:
        return None


def select_openlookeng_yaml_origin_data(package_name, version, mysql):
    """
    Query origin data
    """
    sql = "SELECT id FROM cve_open_lookeng_yaml WHERE package_name = %s " \
          "and version = %s"
    val = (package_name, version)
    packages_data = mysql.getOne(sql, val)
    if packages_data:
        return packages_data
    else:
        return None


def delete_yaml_data():
    """
    delete yaml data
    """
    mysql = Mysql()
    # Delete historical data
    packages_data = select_openeuler_yaml_data(mysql)
    if packages_data is not None:
        for pk in packages_data:
            print("pk: ", pk)
            if pk["package_id"] >= 10000000:
                gauss_data = select_gauss_yaml_origin_data(pk["package_name"], pk["version"], mysql)
                spore_data = select_mindspore_yaml_origin_data(pk["package_name"], pk["version"], mysql)
                lookeng_data = select_openlookeng_yaml_origin_data(pk["package_name"], pk["version"], mysql)
                if not gauss_data and not spore_data and not lookeng_data:
                    print("ID of the currently deleted data: ", pk["git_id"])
                    delete_yaml_openeuler_detail_data(mysql, pk["git_id"])
                    delete_yaml_openeuler_data(mysql, pk["git_id"])
    mysql.close()
